Imports Microsoft.VisualBasic
Imports SubSonic
Imports ClubStarterKit.Data
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient

Namespace ClubStarterKit.Web
    Public Class League

        Public Shared Function FindTeam(ByVal PlayerGuid As Guid) As Teams
            Dim player As New Players(Players.Columns.MemKey, PlayerGuid)
            Dim teamid As Integer
            Dim rdr As IDataReader = SPs.PlayersTeamID(PlayerGuid, Settings.CurrentSeason()).GetReader()

            While rdr.Read()
                teamid = rdr.GetInt32(0)
            End While

            rdr.Close()

            Dim team As New Teams(Teams.Columns.Id, teamid)

            Return team
        End Function


        Public Shared Function LoadGameStats(ByVal PlayerGuid As System.Guid, ByVal SeasonID As Integer) As IDataReader
            Dim rdr As IDataReader = SPs.PlayersSeasonGameData(PlayerGuid, SeasonID).GetReader()
            Return rdr
            rdr.Close()
        End Function

        Public Shared Function LoadSynopsysStats(ByVal PlayerGuid As System.Guid, ByVal SeasonID As Integer) As IDataReader
            Dim rdr As IDataReader = SPs.PlayerSynopsizedStatistics(PlayerGuid, SeasonID).GetReader()
            Return rdr
            rdr.Close()
        End Function

        Public Shared Function ListTeamsSeason(ByVal TeamName As String, ByVal selectedID As Integer) As IDataReader
            Return SPs.TeamSeasonsList(TeamName, selectedID).GetReader()
        End Function

        Public Shared Function LoadTeamStats(ByVal teamid As Integer) As IDataReader
            Return SPs.TeamStats(teamid).GetReader()
        End Function

        Public Shared Function GetSeasonTeamList(ByVal seasonid As Integer) As IDataReader
            Return SPs.SeasonsTeamList(seasonid).GetReader()
        End Function

        Public Shared Function UpcomingSeasonGames(ByVal seasonid As Integer) As IDataReader
            Return SPs.SeasonGames(seasonid, False).GetReader()
        End Function

        Public Shared Function GameStats(ByVal teamid As Integer, ByVal gameid As Integer) As IDataReader
            Return SPs.GameStatistics(gameid, teamid).GetReader()
        End Function

        Public Shared Function GetSeasonsList() As SeasonsCollection
            Dim selectedseason As Integer = Settings.CurrentSeason()

            Dim tempcol As New SeasonsCollection()
            tempcol.LoadAndCloseReader(Seasons.FetchAll())

            Dim realcol As New SeasonsCollection()
            Dim selseason As New Seasons(Seasons.Columns.Id, selectedseason)
            realcol.Add(selseason)

            Dim temp As New Seasons()
            For Each temp In tempcol
                If temp.Id = selectedseason Then
                Else
                    realcol.Add(temp)
                End If
            Next

            Return realcol
        End Function

        Public Shared Function GetSeasonTeams(ByVal seasonid As Integer) As TeamsCollection
            Dim teamcol As New TeamsCollection
            teamcol.LoadAndCloseReader(Teams.FetchByParameter(Teams.Columns.SeasonID, seasonid))

            Return teamcol
        End Function

        Public Shared Function AddGame(ByVal team1 As Integer, ByVal team2 As Integer, ByVal seasonid As Integer, ByVal dte As DateTime) As Integer
            Games.Insert(team1, team2, dte, 0, 0, 0, False)
            Dim gme As New Games(Games.Columns.Dte, dte)

            Dim playercol As New PlayersInTeamsCollection()
            playercol.LoadAndCloseReader(PlayersInTeams.FetchAll())

            Dim plr As New PlayersInTeams()

            For Each plr In playercol
                If plr.TeamID = team1 Or plr.TeamID = team2 Then
                    PlayersInStats.Insert(plr.Players.Id, gme.Id, 0, 0, 0, False)
                End If
            Next

            Return gme.Id
        End Function

        Public Shared Sub UpdateGameScore(ByVal gameid As Integer, ByVal team1score As Integer, ByVal team2score As Integer, ByVal iscomplete As Boolean)
            Dim qry As New Query(Tables.Games)
            qry.AddWhere(Games.Columns.Id, gameid)
            qry.QueryType = QueryType.Update
            qry.AddUpdateSetting(Games.Columns.Team1score, team1score)
            qry.AddUpdateSetting(Games.Columns.Team2score, team2score)
            qry.AddUpdateSetting(Games.Columns.IsComplete, iscomplete)
            qry.Execute()
        End Sub

        Public Shared Sub UpdateStatistic(ByVal statid As Object, ByVal saves As Integer, ByVal catches As Integer, ByVal runs As Integer, ByVal isinjured As Boolean)
            Dim stat As New PlayersInStats(PlayersInStats.Columns.Id, statid)
            If stat.IsLoaded = True Then
                stat.Runs = runs
                stat.Catches = catches
                stat.Saves = saves
                stat.Injured = isinjured
                stat.Save()
            End If
        End Sub

        Public Shared Sub AddPlayerToTeam(ByVal playerMemKey As Object, ByVal teamid As Integer)
            Dim meminf As New MemberInfo(MemberInfo.Columns.Memberid, playerMemKey)
            Dim gi As System.Guid = meminf.Memberid

            Dim player As New Players(Players.Columns.MemKey, gi)
            If player.IsLoaded Then
                PlayersInTeams.Insert(player.Id, teamid)
            Else
                Players.Insert(gi)

                Dim player2 As New Players(Players.Columns.MemKey, gi)
                PlayersInTeams.Insert(player2.Id, teamid)
            End If
        End Sub

        Public Shared Sub AddTeam(ByVal seasonid As String, ByVal teamname As String, ByVal coachmemkey As Object)
            Dim meminf As New MemberInfo(MemberInfo.Columns.Memberid, coachmemkey)
            Teams.Insert(teamname, seasonid, meminf.Memberid)
        End Sub

        Public Shared Sub AddSeason(ByVal startDate As DateTime, ByVal endDate As DateTime, ByVal simplename As String)
            Seasons.Insert(startDate, EndDate, simplename)
        End Sub

        Public Shared Sub MarkSeasonAsCurrent(ByVal seasonid As Integer)
            Dim setting As New WebSettings(WebSettings.Columns.Id, 1)
            If setting.IsLoaded = True Then
                setting.CurrentSeason = seasonid
                setting.Save()
                Settings.PushToSession()
            End If
        End Sub
    End Class
End Namespace